package com.example.taobaolianmeng.ui.activity;

import androidx.annotation.NonNull;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import android.content.ComponentName;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.MenuItem;

import com.example.taobaolianmeng.R;
import com.example.taobaolianmeng.base.BaseActivity;
import com.example.taobaolianmeng.base.BaseFragment;
import com.example.taobaolianmeng.ui.fragment.HomeFragment;
import com.example.taobaolianmeng.ui.fragment.OnSellFragment;
import com.example.taobaolianmeng.ui.fragment.SearchFragment;
import com.example.taobaolianmeng.ui.fragment.SelectedFragment;
import com.google.android.material.bottomnavigation.BottomNavigationView;

import butterknife.BindView;

public class MainActivity extends BaseActivity {

    private static final String TAG = "MainActivity";
    @BindView(R.id.main_navigation_bar)
    public BottomNavigationView mNavigationView;
    private HomeFragment mHomeFragment;
    private OnSellFragment mRedPacketFragment;
    private SelectedFragment mSelectedFragment;
    private SearchFragment mSearchFragment;
    private FragmentManager mFm;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    protected void initPresenter() {

    }

    @Override
    protected void initView() {
        initFragment();
    }

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_main;
    }

    @Override
    protected void initEvent() {
        initListener();
    }

    private void initFragment() {
        mHomeFragment = new HomeFragment();
        mRedPacketFragment = new OnSellFragment();
        mSelectedFragment = new SelectedFragment();
        mSearchFragment = new SearchFragment();
        mFm = getSupportFragmentManager();
        switchFragment(mHomeFragment);  //默认选中第一页
    }

    private void initListener() {
        mNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                Log.d(TAG,"titile->"+item.getTitle()+"id->"+item.getItemId());
                if (item.getItemId() == R.id.home) {
                    switchFragment(mHomeFragment);
                } else if(item.getItemId() == R.id.selected){
                    switchFragment(mSelectedFragment);
                }else if(item.getItemId() == R.id.red_packet){
                    switchFragment(mRedPacketFragment);
                }else if(item.getItemId() == R.id.search){
                    switchFragment(mSearchFragment);
                }
                return true;
            }
        });
    }

    //上一次显示的fragment
    private BaseFragment lastOneFragment = null;

    // 实现切换
    private void switchFragment(BaseFragment targetFragment) {
        //如果上一个fragment跟当前要切换的fragment是同一个，那么不需要切换
        if(lastOneFragment == targetFragment) {
            return;
        }
        //修改成add和hide的方式控制Fragment的切换
        FragmentTransaction fragmentTransaction = mFm.beginTransaction();

        if(!targetFragment.isAdded()){
            fragmentTransaction.add(R.id.main_page_container,targetFragment);
        }else {

            fragmentTransaction.show(targetFragment);
        }

        if (lastOneFragment != null) {
            fragmentTransaction.hide(lastOneFragment);
        }
        lastOneFragment =targetFragment;



//        fragmentTransaction.replace(R.id.main_page_container,targetFragment);

        fragmentTransaction.commit();

    }


}